//-----------------------------------------------------------------------------
// Name: Input.cpp
// Auth: Bradford J. Smith
// Desc: contains all call back functions related to keyboard and mouse 
//	handling
//-----------------------------------------------------------------------------

#include "Input.h"

#include "Assert.h"
#include "DisplayManager.h"
#include "InputManager.h"
#include "Jet.h"
#include "Player.h"
#include "LittleGuy.h"
#include "SharedConstants.h"
#include "SharedFunctions.h"
#include "Systems.h"
#include "TuningParser.h"

#include <GL/glut.h>
#include <GL/freeglut_ext.h>

//-----------------------------------------------------------------------------
// Function Definitions:
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
void KeyDownFunction(GLubyte key, GLint x, GLint y)
{
	GetInputManager()->HandleKeyDown(key, x, y);
	cDisplayManager* pDisplayManager = GetDisplayManager();

	// press any key to exit the function
	switch(key) 
	{
	case 'r':
		{
			// reload tuning
			GetGeneralTuning()->ReparseFile();
      cLittleGuy* pLittleGuy = GetLittleGuy();
      pLittleGuy->MoveTo( 
        (float) RandomX( (int) kSides ) ,
        (float) RandomY( (int) kSides ) ,
        kStart + pLittleGuy->getRadius() );
		}
		break;
	case '1':
		{
			// switch to orthographic mode
			pDisplayManager->SetOrthographicMode();
		}
		break;
	case '2':
		{
			// switch to orthographic mode
			pDisplayManager->SetPerspectiveMode();
		}
		break;
  case 'd':
    {
      float heading = pDisplayManager->GetCameraHeading();
		  pDisplayManager->SetCameraHeading(heading + 1.0f);
    }
    break;
  case'a':
    {
      float heading = pDisplayManager->GetCameraHeading();
		  pDisplayManager->SetCameraHeading(heading - 1.0f);
    }
		break;
  case 'w':
    {
      float pitch = pDisplayManager->GetCameraPitch();
      pDisplayManager->SetCameraPitch( pitch + 0.5f );
    }
    break;
  case 's':
    {
      float pitch = pDisplayManager->GetCameraPitch();
      pDisplayManager->SetCameraPitch( pitch - 0.5f );
    }
    break;
	case 'q':
  case 'Q':
		glutLeaveMainLoop();
	}
	// Refresh the Window
	glutPostRedisplay();
}

//-----------------------------------------------------------------------------
void KeyUpFunction(GLubyte key, GLint x, GLint y)
{
	GetInputManager()->HandleKeyUp(key, x, y);

	// Refresh the Window
	glutPostRedisplay();
}



////-----------------------------------------------------------------------------
//void SpecialKeyDownFunction(GLint key, GLint x, GLint y)
//{
//	GetInputManager()->HandleKeyDownSpecial(key, x, y);
//
//	cPlayer* pPlayer = GetPlayer();
//
//	switch (key)
//	{
//	case GLUT_KEY_LEFT:
//		pPlayer->Move( (float) kStartTurnLeft);
//		break;
//	case GLUT_KEY_RIGHT:
//		pPlayer->Move( (float) kStartTurnRight);
//		break;
//	case GLUT_KEY_UP:
//		pPlayer->Move( (float) kStartMoveForward);
//		break;
//	case GLUT_KEY_DOWN:
//		pPlayer->Move( (float) kStartMoveBackward);
//		break;
//	};
//
//	// Refresh the Window
//	glutPostRedisplay();
//}
//
////-----------------------------------------------------------------------------
//void SpecialKeyUpFunction(GLint key, GLint x, GLint y)
//{
//	GetInputManager()->HandleKeyUpSpecial(key, x, y);
//
//	cPlayer* pPlayer = GetPlayer();
//
//	switch (key)
//	{
//	case GLUT_KEY_LEFT:
//		pPlayer->Move( (float) kStopTurnLeft);
//		break;
//	case GLUT_KEY_RIGHT:
//		pPlayer->Move( (float) kStopTurnRight);
//		break;
//	case GLUT_KEY_UP:
//		pPlayer->Move( (float) kStopMoveForward);
//		break;
//	case GLUT_KEY_DOWN:
//		pPlayer->Move( (float) kStopMoveBackward);
//		break;
//	};
//
//	// Refresh the Window
//	glutPostRedisplay();
//}

//-----------------------------------------------------------------------------
void MouseMoveFunction(GLint x, GLint y)
{
  //GLdouble* objx, objy, objz;

	// convert mouse coordinates to raster coordinates:
	y = GetDisplayManager()->GetWindowHeight() - y;

	cPlayer* pPlayer = GetPlayer();
	if (pPlayer)
	{
    /*if ( gluUnProject( (GLdouble) x, (GLdouble) y, 0.5, 
                        xxx, xxx, xxx, 
                        objx, objy, objz ) )*/
      pPlayer->MouseMove((float) x, (float) y );
	}
}

//-----------------------------------------------------------------------------
void MouseClickFunc(GLint button, GLint state, GLint x, GLint y)
{
	cInputManager* pInputManager = GetInputManager();
	pInputManager->HandleMouseButton(button, state, x, y);
  cPlayer* pPlayer = GetPlayer();
  cLittleGuy* pLittleGuy = GetLittleGuy();

	// handle mouse clicks
	if (state == GLUT_DOWN)
	{
		switch (button)
		{
		case GLUT_LEFT_BUTTON:
			// draw a debug pick line
      pPlayer->shoot();
      pLittleGuy->shoot();
			break;
		}
	}

}
